﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyHealthByDesignWebsite.Models
{
    public class MyHealthHistoryRepository
    {
        //Description:  Repository for health history
        //Created:  Nov 2011
        //Programmer:  Bob Whiteis
        private FullDataSetDataContext db = new FullDataSetDataContext();
        //
        //
        //pre-condition:  health history id had been supplied
        //post-condition: health history has been returned
        public UserHealthHistory GetUserHealthHistory(int id)
        {
            return db.UserHealthHistories.SingleOrDefault(uuh => uuh.U_UserID == id);
        }


        //get HealthHistoryFieldDesc by HHFD_fieldDescID
        //pre-condition:  health history field desc  id had been supplied
        //post-condition: health history field descriptions have been returned
        public HealthHistoryFieldsDesc GetHealthHistoryFieldDesc(string hhfd_fieldDescID)
        {
            return db.HealthHistoryFieldsDescs.SingleOrDefault(hhfd => hhfd.HHFD_fieldDescID == hhfd_fieldDescID);
        }


        //pre-condition:  health history field id had been supplied
        //post-condition: health history fields have been returned
        public IQueryable<HealthHistoryFieldsDesc> GetHealthHistoryDescByType(string hh_fieldID)
        {
            return from healthHistoryFieldsDesc in db.HealthHistoryFieldsDescs
                   where healthHistoryFieldsDesc.HHF_fieldID == hh_fieldID
                   orderby healthHistoryFieldsDesc.HHF_fieldID
                   select healthHistoryFieldsDesc;
        }

        //
        // Insert/Delete Methods
        //pre-condition:  user health history has been supplied
        //post-conditon user health history has been added
        public void Add(UserHealthHistory uhh)
        {
            db.UserHealthHistories.InsertOnSubmit(uhh);
        }
        //pre-condition:  user health history has been supplied
        //post-conditon user health history has been deleted
        public void Delete(UserHealthHistory uhh)
        {

            db.UserHealthHistories.DeleteOnSubmit(uhh);
        }

        // Insert/Delete Methods
        //pre-condition:  user health history field desc has been supplied
        //post-conditon user health history field desc has been inserted
        public void AddUserHealthHistoryFields(UserHealthHistoryFieldsDesc uhhfd)
        {
            db.UserHealthHistoryFieldsDescs.InsertOnSubmit(uhhfd);
        }


        //
        // Persistence
        public void Save()
        {

            db.SubmitChanges();

        }    
    }
}